package com.treasure.web.command.event;

import com.treasure.common.exception.ParameterException;
import com.treasure.common.types.FileName;
import com.treasure.domain.domain.entity.document.UploadFileEntity;
import com.treasure.common.model.UploadDocumentRequest;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @author sunhongmin
 * @date 2023/10/21 09:39
 * @description
 */
@Getter
public class DocumentUploadEvent extends ApplicationEvent {

    private final UploadDocumentRequest uploadDocumentRequest;

    private final File file;

    private final FileName fileName;

    public DocumentUploadEvent(Object source, UploadDocumentRequest uploadDocumentRequest, UploadFileEntity uploadFileEntity) throws IOException {
        super(source);
        this.uploadDocumentRequest = uploadDocumentRequest;
        this.file = uploadFileEntity.convertToLocalFile(uploadDocumentRequest.getCorpId());
        this.fileName = uploadFileEntity.getFileName();
    }

    public void validate() {
        if (Objects.isNull(uploadDocumentRequest)) {
            throw new ParameterException("uploadDocumentRequest 为空");
        }
        if (Objects.isNull(file)) {
            throw new ParameterException("上传文件为空");
        }
    }

}
